from flask import Flask
import arrow
from bson import ObjectId
from datetime import date


def override_json_encoder(app: Flask):
    superclass = app.json_encoder

    class _JsonEncoder(superclass):
        def default(self, o):
            if isinstance(o, ObjectId):
                return str(o)
            if isinstance(o, date):
                return arrow.Arrow.fromdatetime(o).for_json()
            return superclass.default(self, o)

    app.json_encoder = _JsonEncoder
